package com.visionet.school.app.web.repository.member;

import com.visionet.school.app.web.entity.member.MemberClass;
import com.visionet.school.app.web.entity.member.vo.MemberClassVo;
import com.visionet.school.app.web.entity.vo.MemberVo;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by dzd on 2017/3/3.
 */
@Repository
public interface MemberClassRepository extends PagingAndSortingRepository<MemberClass,String>,JpaSpecificationExecutor<MemberClass> {

    /**
     * 根据班级ID查询班主任Id
     * @param classId
     * @return
     */
    @Query("from MemberClass as mc where mc.classId = ?1 and mc.memberType = '1' and mc.isDeleted = 0")
    MemberClass findTeacherByClassId(String classId);

    /**
     * 根据学生ID查询所在班级关系
     * @param memberId
     * @return
     */
    @Query("from MemberClass as mc where mc.memberId = ?1 and mc.isDeleted = 0")
    MemberClass findOneByMemberId(String memberId);

    //根据不同的类型和班级查询成员集合
    @Query("from MemberClass as mc where mc.classId = ?1 and mc.memberType =?2 and mc.isDeleted = 0")
    List<MemberClass> findListByClassId(String classId, String memberType);

    //查询班级所有人，包括老师
    @Query("select count(mc.id) from MemberClass as mc where mc.isDeleted = 0 and mc.classId = ?1")
    int countMemberClass(String classId);

    //查询班级所有人，根据类型
    @Query("select count(mc.id) from MemberClass as mc where mc.isDeleted = 0 and mc.classId = ?1 and mc.memberType = ?2")
    int countMemberClassByType(String classId,String memberType);

    /**
     * 查询成员所属的班级关系
     * @param memberId
     * @param memberType
     * @return
     */
    @Query("from MemberClass as mc where mc.isDeleted = 0 and mc.memberId = ?1 and mc.memberType = ?2 ")
    List<MemberClass> findByMemberIdAndType(String memberId, String memberType);


    Page<MemberVo> findStudentByClassId(MemberClassVo memberClassVo);
}
